Python3 访问 webservice 接口(WSDL+suds 方式)

您所在的位置:网站首页 python 调用接口取数据 Python3 访问 webservice 接口(WSDL+suds 方式)

Python3 访问 webservice 接口(WSDL+suds 方式)

2023-12-21 04:23| 来源: 网络整理| 查看: 265

Python3 访问 webservice 接口(WSDL+suds 方式)

suds 安装 参考 suds 使用说明 WSDL文件解读 学习 python 学习

上一篇文章整理了 通过request 方式直接访问 webservice 接口并解析结果xml 信息 的方式,基于系统接口安全性的考虑,webservice 的接口不会直接对外开放,会通过wsdl 的封装转换方式对外开放接口,也因此 本次对python 通过 wsdl 的方式访问 webservice 接口进行整理。

1、需要安装suds 依赖工具: pip install suds

也许使用以上命令执行后会出现如下错误:

(testEnv) ******:~# pip install suds Collecting suds Downloading https://files.pythonhosted.org/packages/bc/d6/960acce47ee6f096345fe5a7d9be7708135fd1d0713571836f073efc7393/suds-0.4.tar.gz (104kB) 100% |████████████████████████████████| 112kB 95kB/s Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/tmp/pip-install-almbxsaw/suds/setup.py", line 20, in import suds File "/tmp/pip-install-almbxsaw/suds/suds/__init__.py", line 154, in import client ModuleNotFoundError: No module named 'client' ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-almbxsaw/suds/

先莫慌,很可能是因为你的python 更新到3以上了,查了些其他朋友的博客:

pip install suds-jurko //python3.x 后官方更名为: suds-jurko 2、开始编写代码访问url 并打印出 client 结果: #引入依赖对象 import time from suds.client import Client from suds.sudsobject import asdict #选择webservice 接口 url #!!!注意,使用前一定要检查 url 是否正常 sap_url_ = "http://localhost/dir/wsdl?p=ic/63e6c57d3" #编写方法进行访问 def obtain_by_serial(serial, device_id, origin): try: #添加用户认证也是相当简单,当然,如果没有说明文件估计也是丈二的和尚, #我也是找了好久才找到认证用户和密码的正确填写方式 client = Client(sap_url_, username='QA', password='AQ123') print(client) except Exception as e: print(e) return def get_current_timestamp(): return int(round(time.time() * 1000))

先调试 print 出client 内容如下 :

Suds ( https://fedorahosted.org/suds/ ) version: 0.6 Service ( SY_OBService ) tns="urn:QA:pi:K2:Zget_equi" Prefixes (2) ns0 = "urn:sap-com:document:sap:rfc:functions" ns1 = "urn:sap-com:document:sap:soap:functions:mc-style" Ports (2): (HTTP_Port) Methods (1): Zget_equi_SY_OB(ns1:ZInput PiInput) Types (22): ns1:ZInput

由上面的打印内容 可以看出service 下面的 method 部分 即接口方法名和参数 以及参数类型

Zget_equi_SY_OB(ns1:ZInput PiInput)

即调用方法名为:Zget_equi_SY_OB 参数类型为:ZInput 参数名为:PiInput

3、继续构造参数,传参调用接口方法:

#!!参数的具体类型和结构,需要阅读 WSDL 文件 ,根据 WSDL 内容进行构造

def obtain_by_serial(serial, device_id, origin): try: client = Client(sap_url_, username='P8XAMP', password='00sGks3Zsd#D') print(client) #!!!!!参数的具体类型和结构,需要阅读 WSDL 文件 ,根据 WSDL 内容进行构造 ZInput = { 'Messageid': str(get_current_timestamp()), 'Doc': { 'item': { 'Equnr': device_id, 'Sernr': serial, 'Buffer1': '', 'Buffer2': '', 'Buffer3': '', 'Buffer4': '', 'Buffer5': '', 'Buffer6': '', 'Buffer7': '', 'Buffer8': '', 'Buffer9': '', 'Buffer10': '', } } } #传参调用接口 response = client.service.Zget_equi_SY_OB(ZInput) #打印返回结果 print(response) except Exception as e: print(e) return

至此 就结束了

以上均为自己采坑记录,如有错误还希望留言指出 i!@^_^@!i



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3